package com.oldking.utils;

import java.io.File;
import java.util.List;

public class ConvertVideo {


    public static void main(String[] args) {
        //processmp4 是表示不要原音 flag, 视频路径 path, 输出视频路径 outFilePath, mp3路径 mp3Path, 字幕路径 subtitlePath, 开始时间 startTime, 结束时间 endTime
        boolean flag = false;
        String path = "F:\\video\\glzx.mkv";
        String outFilePath = "F:\\video\\999.mp4";
        String mp3Path = "";
        String subtitlePath = "";
        String startTime = "0:1:30";
        String endTime = "0:0:50";

        composeVideo(flag, path, outFilePath, mp3Path, subtitlePath, startTime, endTime);


    }

    private static void composeVideo(boolean flag, String path, String outFilePath, String mp3Path, String subtitlePath, String startTime, String endTime) {
        if (!checkfile(path)) {
            System.out.println(path + " is not file");
            return;
        }
        if (process(flag, path, outFilePath, mp3Path, subtitlePath, startTime, endTime)) {
            System.out.println("ok");
        }
    }

    private static boolean process(boolean flag, String path, String outFilePath, String mp3Path, String subtitlePath, String startTime, String endTime) {
        int type = checkContentType(path);
        boolean status = false;
        if (type == 0) {
            status = processmp4(flag, path, outFilePath, mp3Path, subtitlePath, startTime, endTime);// 直接将文件转为flv文件
        } else if (type == 1) {
            String avifilepath = processAVI(type, path);
            if (avifilepath == null)
                return false;// avi文件没有得到
            status = processmp4(flag, path, outFilePath, mp3Path, subtitlePath, startTime, endTime);// 将avi转为flv
        }
        return status;
    }

    private static int checkContentType(String path) {
        String type = path.substring(path.lastIndexOf(".") + 1,
                path.length()).toLowerCase();
        // ffmpeg能解析的格式：（asx，asf，mpg，wmv，3gp，mp4，mov，avi，flv等）
        if (type.equals("avi")) {
            return 0;
        } else if (type.equals("mpg")) {
            return 0;
        } else if (type.equals("wmv")) {
            return 0;
        } else if (type.equals("3gp")) {
            return 0;
        } else if (type.equals("mov")) {
            return 0;
        } else if (type.equals("mp4")) {
            return 0;
        } else if (type.equals("asf")) {
            return 0;
        } else if (type.equals("asx")) {
            return 0;
        } else if (type.equals("flv")) {
            return 0;
        } else if (type.equals("mkv")) {
            return 0;
        }
        //对ffmpeg无法解析的文件格式(wmv9，rm，rmvb等),
        //可以先用别的工具（mencoder）转换为avi(ffmpeg能解析的)格式.
        else if (type.equals("wmv9")) {
            return 1;
        } else if (type.equals("rm")) {
            return 1;
        } else if (type.equals("rmvb")) {
            return 1;
        }
        return 9;
    }

    private static boolean checkfile(String path) {
        File file = new File(path);
        if (!file.isFile()) {
            return false;
        }
        return true;
    }

    //对ffmpeg无法解析的文件格式(wmv9，rm，rmvb等), 可以先用别的工具（mencoder）转换为avi(ffmpeg能解析的)格式.
    private static String processAVI(int type, String path) {
        try {
            File file = new File("");
            List<String> commend = new java.util.ArrayList<String>();
            commend.add(file.getCanonicalPath() + "/src/main/resources/mencoder/mencoder.exe");
            commend.add(path);
            commend.add("-oac");
            commend.add("lavc");
            commend.add("-lavcopts");
            commend.add("acodec=mp3:abitrate=64");
            commend.add("-ovc");
            commend.add("xvid");
            commend.add("-xvidencopts");
            commend.add("bitrate=600");
            commend.add("-of");
            commend.add("avi");
            commend.add("-o");
            String tempFiledir = path.substring(path.lastIndexOf(".") + 1, path.length());
            File tempDir = new File(tempFiledir);
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            String tmep = tempFiledir + "/" + UUIDUtil.getUUID() + ".avi";
            commend.add(tmep);

            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.start();
            return tmep;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //ffmpeg能解析的格式：（asx，asf，mpg，wmv，3gp，mp4，mov，avi，flv等）
    private static boolean processmp4(boolean flag, String path, String outFilePath, String mp3Path, String subtitlePath, String startTime, String endTime) {
        if (!checkfile(path)) {
            System.out.println(path + " is not file");
            return false;
        }
        try {
            //        1.去除视频原音频
            //        ffmpeg -i a.MOV -vcodec copy -an a.mp4
            //        2.追加字幕与音频 并且设置字幕字体大小
            //        ffmpeg -i a.mp4 -i a.mp3 -vf "subtitles=a.srt:force_style='Fontsize=24'" out.mp4

            List<String> commend = new java.util.ArrayList<String>();
            File file = new File("");
            commend.add(file.getCanonicalPath() + "/src/main/resources/ffmpeg/bin/ffmpeg.exe");
            commend.add("-ss");
            commend.add(startTime);
            commend.add("-t");
            commend.add(endTime);
            commend.add("-i");
            commend.add(path);
            commend.add("-c");
            if (flag) {
                commend.add("-vcodec");
                commend.add("-an");
            } else {
                commend.add("-i");
                commend.add(mp3Path);
                commend.add("-vf");
                String sbutitie = "subtitles=" + subtitlePath + ":force_style='Fontsize=24'";
                commend.add(sbutitie);
            }
            commend.add("copy");
            commend.add("-map");
            commend.add("0");
            commend.add(outFilePath);

            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.start();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}